package com.cetc.common.validate;

import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * Created by Administrator on 2018/9/21.
 */
public class IntegerTypeValidator implements ConstraintValidator<IntegerType, String> {

    int max;
    int min;

    @Override
    public void initialize(IntegerType constraintAnnotation) {
        min = constraintAnnotation.min();
        max = constraintAnnotation.max();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if(null == value || value.equals("")){
            return true;
        }

        if(!StringUtils.isNumeric(value)){
            return false;
        }
        else{
            if(Integer.parseInt(value) < min){
                return false;
            }
            if(Integer.parseInt(value) > max){
                return false;
            }
        }

        return true;
    }
}
